<?php

/**
 * This is the model class for table "{{cmp_payment}}".
 *
 * The followings are the available columns in table '{{cmp_payment}}':
 * @property integer $zcp_id
 * @property integer $zm_id
 * @property integer $zpy_id
 * @property string $zcp_config
 * @property integer $zcp_status
 */
class CmpPayment extends CActiveRecord
{
	//用户名
	public $zcp_payment_user;
	//财富通商账号
	public $zcp_payment_id;
	//密匙
	public $zcp_payment_key;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpPayment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_payment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zpy_id', 'required'),
			array('zm_id, zpy_id, zcp_status', 'numerical', 'integerOnly'=>true),
			array('zcp_config', 'safe'),
			array('zcp_payment_user,zcp_payment_id,zcp_payment_key', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcp_id, zm_id, zpy_id, zcp_config, zcp_status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcp_id' => 'Zcp',
			'zm_id' => 'Zm',
			'zpy_id' => 'Zpy',
			'zcp_config' => 'Zcp Config',
			'zcp_status' => 'Zcp Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcp_id',$this->zcp_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpy_id',$this->zpy_id);
		$criteria->compare('zcp_config',$this->zcp_config,true);
		$criteria->compare('zcp_status',$this->zcp_status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 是否启动
	 */
	public function getStatus($type=null){
		if($type==null){
			return array(
				'1'=>'是',
				'2'=>'否'
			);
		}else{
			$level = $this->getStatus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作 将本配置信息系列化保存
	 */
	protected function beforeSave(){
		if(parent::beforeSave()){
			$this->zcp_config = serialize(array(
					'zcp_payment_user'=>$this->zcp_payment_user,
					'zcp_payment_id'=>$this->zcp_payment_id,
					'zcp_payment_key'=>$this->zcp_payment_key,
			));
			$this->zm_id = Company::model()->getCmpUserId();
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 读取数据后的操作，将配置信息取出并反系列化
	 */
	protected function afterFind(){
		$payment = unserialize($this->zcp_config);
		$this->zcp_payment_user = $payment['zcp_payment_user'];
		$this->zcp_payment_id = $payment['zcp_payment_id'];
		$this->zcp_payment_key = $payment['zcp_payment_key'];
		return true;
	}
	
	
}